/**
 * problem_042.cpp
 * Copyright (C) 2011 - xrose
 */

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int isTriangle(const int tn)
{
	//if tn = n(n+1)/2 is a triangle number then n = sqrt(2*n)
	int n = int(sqrt(2*tn));
	return tn==n*(n+1)/2;
}
int isCharacter(char c)
{
    if(('A'<=c)&&(c<='Z'))
        return c-'A'+1;
    if(('a'<=c)&&(c<='z'))
        return c-'a'+1;
    return 0;
}
int isTriangleW(const char* wrd)
{
	int lw = strlen(wrd);
	int sum = 0;
	for(int i=0; i<lw; i++)
	{
        sum+=isCharacter(wrd[i]);
	}
	cout<<sum<<endl;
	return isTriangle(sum);
}
int main (int argc, char const* argv[])
{
    cout<<isTriangleW("SKY");
    char wrd[80];
    FILE * pFile;
    long lSize;
    char * buffer;
    size_t result;
    char * pch;
    int sumz = 0;

    pFile = fopen ("words.txt","r");
    // obtain file size:
    fseek (pFile , 0 , SEEK_END);
    lSize = ftell (pFile);
    rewind (pFile);

    // allocate memory to contain the whole file:
    buffer = (char*) malloc (sizeof(char)*lSize);
    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

    // copy the file into the buffer:
    result = fread (buffer,1,lSize,pFile);
    if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

    pch = strtok (buffer,"\",");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        if(isTriangleW(pch)) sumz++;
        cout<<sumz<<endl;
        pch = strtok (NULL, "\",");
//        getchar();
    }

    fclose (pFile);
    free(buffer);
	return 0;
}
